#include "air780e_common.h"
#include "pika_hal_mobile.h"
#include "luat_mobile.h"

int pika_hal_mobile_get_imei(int sim_id, char* out, int maxlen){
    return luat_mobile_get_imei(sim_id, out, 15) > 0 ? 0 : -1;
}

int pika_hal_mobile_get_imsi(int sim_id, char* out, int maxlen){
    return luat_mobile_get_imsi(sim_id, out, 20) > 0 ? 0 : -1;
}

int pika_hal_mobile_get_iccid(int sim_id, char* out, int maxlen){
    return luat_mobile_get_iccid(sim_id, out, 20) > 0 ? 0 : -1;
}   

int pika_hal_mobile_get_number(int sim_id, char* out, int maxlen){
    return luat_mobile_get_sim_number(sim_id, out, 20) > 0 ? 0 : -1;
}


int pika_hal_mobile_get_sim_id(void){
    int sim_id;
    int res = luat_mobile_get_sim_id(&sim_id);
    return res == 0 ? sim_id : -1; 
}

int pika_hal_mobile_get_sim_status(void){
    LUAT_MOBILE_SIM_STATUS_E status = luat_mobile_get_sim_status();
    int res = -1;
    switch (status)
    {
    case LUAT_MOBILE_SIM_READY:
        res = PIKA_HAL_MOBILE_SIM_READY;
        break;
    case LUAT_MOBILE_NO_SIM:
        res = PIKA_HAL_MOBILE_NO_SIM;
        break;
    case LUAT_MOBILE_SIM_NEED_PIN:
        res = PIKA_HAL_MOBILE_SIM_NEED_PIN;
        break;
    default:
        break;
    }
    return res;
}

void pika_hal_mobile_set_rrc_time(int sec){
    luat_mobile_set_rrc_auto_release_time(sec);
}

int pika_hal_mobile_set_apn(int sim_id, int cid, char* apn_name, char * user_name, char* password, int ip_type, int protocol){
    int res;
    res = luat_mobile_set_apn_base_info(sim_id, cid, ip_type, apn_name, strGetSize(apn_name));
    if (res <= 0){
        return -1;
    }
    res = luat_mobile_set_apn_auth_info(sim_id, cid, protocol, user_name, strGetSize(user_name), password, strGetSize(password));
    if (res <= 0){
        return -1;
    }
    return 0;
}

int pika_hal_mobile_active_apn(int sim_id, int cid, int enable){
    return luat_mobile_active_apn(sim_id, cid, 1) > 0 ? 0 : -1;
}

int pika_hal_mobile_get_apn(int sim_id, int cid, char* apn_name, int maxlen){
    return luat_mobile_get_apn(sim_id, cid, apn_name, maxlen) > 0 ? 0 : -1;
}

int pika_hal_mobile_set_flymode(int sim_id, int mode){
    return luat_mobile_set_flymode(sim_id, mode) == 0 ? 0 : -1;
}

int pika_hal_mobile_get_flymode(int sim_id){
    int mode = luat_mobile_get_flymode(sim_id);
    if (mode < 0){
        return -1;
    }
    return mode;
}

void pika_hal_mobile_set_ipv6(int enable){
    luat_mobile_set_default_pdn_ipv6(enable);
}
int pika_hal_mobile_get_ipv6(void){
    return luat_mobile_get_default_pdn_ipv6();
}

void pika_hal_mobile_set_sync_time(int enable){
    luat_mobile_set_sync_time(enable);
}

int pika_hal_mobile_get_sync_time(void){
    return luat_mobile_get_sync_time() == 0 ? 0 : 1;
}

int pika_hal_mobile_get_register_status(void){
    /**
    -- 状态描述
    -- 0:网络未注册
    -- 1:网络已注册
    -- 2:正在搜网中
    -- 3:网络注册被拒绝
    -- 4:网络状态未知
    -- 5:漫游,且已注册
    -- 6:仅SMS可用
    -- 7:仅SMS可用,且漫游状态
    -- 8:仅紧急呼叫. 注意, 国内不支持此状态,模块也不支持紧急呼叫
     */
    int status = luat_mobile_get_register_status();
    int res = -1;

    switch (status)
    {
    case LUAT_MOBILE_STATUS_UNREGISTER:
        res = PIKA_HAL_MOBILE_UNREGISTER;
        break;
    case LUAT_MOBILE_STATUS_REGISTERED:
        res = PIKA_HAL_MOBILE_REGISTERED;
        break;
    case LUAT_MOBILE_STATUS_INSEARCH:
        res = PIKA_HAL_MOBILE_SEARCHING;
        break;
    case LUAT_MOBILE_STATUS_DENIED:
        res = PIKA_HAL_MOBILE_REGISTER_DENIED;
        break;
    case LUAT_MOBILE_STATUS_UNKNOW:
        res = PIKA_HAL_MOBILE_STATUS_UNKNOW;
        break;
    case LUAT_MOBILE_STATUS_REGISTERED_ROAMING:
        res = PIKA_HAL_MOBILE_REGISTERED_ROAMING;
        break;
    case LUAT_MOBILE_STATUS_SMS_ONLY_REGISTERED:
        res = PIKA_HAL_MOBILE_SMS_ONLY_REGISTERED;
        break;
    case LUAT_MOBILE_STATUS_SMS_ONLY_REGISTERED_ROAMING:
        res = PIKA_HAL_MOBILE_SMS_ONLY_REGISTERED_ROAMING;
        break;
    case LUAT_MOBILE_STATUS_EMERGENCY_REGISTERED:
        res = PIKA_HAL_MOBILE_EMERGENCY_REGISTERED;
        break;
    case LUAT_MOBILE_STATUS_CSFB_NOT_PREFERRED_REGISTERED:
        res = PIKA_HAL_MOBILE_CSFB_NOT_PREFERRED_REGISTERED;
        break;
    case LUAT_MOBILE_STATUS_CSFB_NOT_PREFERRED_REGISTERED_ROAMING:
        res = PIKA_HAL_MOBILE_CSFB_NOT_PREFERRED_REGISTERED_ROAMING;
        break;
    default:
        break;
    }
    return res;
}

int pika_hal_mobile_get_cell_info_async(int timeout){
    return luat_mobile_get_cell_info_async(timeout) == 0 ? 0 : -1;
}

static void _copy_lte_cell_info(luat_mobile_cell_info_t* luat_cell_info, pika_hal_mobile_lte_cell_info * lte_cell_info){
    // 装填主服务小区
    lte_cell_info->neighbor_info_num = luat_cell_info->lte_neighbor_info_num;
    pika_hal_mobile_lte_service_cell_info *service_info = &lte_cell_info->service_info;
    service_info->cid = luat_cell_info->lte_service_info.cid;
    service_info->mcc = luat_cell_info->lte_service_info.mcc;
    service_info->mnc = luat_cell_info->lte_service_info.mnc;
    service_info->tac = luat_cell_info->lte_service_info.tac;
    service_info->pci = luat_cell_info->lte_service_info.pci;
    service_info->earfcn = luat_cell_info->lte_service_info.earfcn;
    service_info->rssi = luat_cell_info->lte_service_info.rssi;
    service_info->rsrp = luat_cell_info->lte_service_info.rsrp;
    service_info->rsrq = luat_cell_info->lte_service_info.rsrq;
    service_info->snr = luat_cell_info->lte_service_info.snr;
    service_info->is_tdd = luat_cell_info->lte_service_info.is_tdd;
    service_info->band = luat_cell_info->lte_service_info.band;
    service_info->ulbandwidth = luat_cell_info->lte_service_info.ulbandwidth;
    service_info->dlbandwidth = luat_cell_info->lte_service_info.dlbandwidth;
    
    //装填邻小区
    pika_hal_mobile_lte_neighbor_cell_info *neighbor_info;
    luat_mobile_lte_cell_info_t *luat_neighbor_cell_info;
    
    for (int i = 0; i < luat_cell_info->lte_neighbor_info_num; i++){
        neighbor_info = &lte_cell_info->neighbor_infos[i];
        luat_neighbor_cell_info = &luat_cell_info->lte_info[i];
        neighbor_info->cid = luat_neighbor_cell_info->cid;
        neighbor_info->mcc = luat_neighbor_cell_info->mcc;
        neighbor_info->mnc = luat_neighbor_cell_info->mnc;
        neighbor_info->tac = luat_neighbor_cell_info->tac;
        neighbor_info->pci = luat_neighbor_cell_info->pci;
        neighbor_info->earfcn = luat_neighbor_cell_info->earfcn;
        neighbor_info->rsrp = luat_neighbor_cell_info->rsrp;
        neighbor_info->rsrq = luat_neighbor_cell_info->rsrq;
        neighbor_info->snr = luat_neighbor_cell_info->snr;

    }
}

static void _copy_gsm_cell_info(luat_mobile_cell_info_t* luat_cell_info, pika_hal_mobile_gsm_cell_info * gsm_cell_info){
    gsm_cell_info->neighbor_info_num = luat_cell_info->gsm_neighbor_info_num;
    // 装填主服务小区
    pika_hal_mobile_gsm_service_cell_info *service_info = &gsm_cell_info->service_info;
    service_info->cid = luat_cell_info->gsm_service_info.cid;
    service_info->mcc = luat_cell_info->gsm_service_info.mcc;
    service_info->mnc = luat_cell_info->gsm_service_info.mnc;
    service_info->lac = luat_cell_info->gsm_service_info.lac;
    service_info->arfcn = luat_cell_info->gsm_service_info.arfcn;
    service_info->bsic = luat_cell_info->gsm_service_info.bsic;
    service_info->rssi = luat_cell_info->gsm_service_info.rssi;
    
    //装填邻小区
    pika_hal_mobile_gsm_neighbor_cell_info *neighbor_info;
    luat_mobile_gsm_cell_info_t *luat_neighbor_cell_info;
    
    for (int i = 0; i < luat_cell_info->gsm_neighbor_info_num; i++){
        neighbor_info = &gsm_cell_info->neighbor_infos[i];
        luat_neighbor_cell_info = &luat_cell_info->lte_info[i];

        neighbor_info->cid = luat_neighbor_cell_info->cid;
        neighbor_info->mcc = luat_neighbor_cell_info->mcc;
        neighbor_info->mnc = luat_neighbor_cell_info->mnc;
        neighbor_info->lac = luat_neighbor_cell_info->lac;
        neighbor_info->arfcn = luat_neighbor_cell_info->arfcn;
        neighbor_info->bsic = luat_neighbor_cell_info->bsic;
        neighbor_info->rssi = luat_neighbor_cell_info->rssi;
    }
}

int pika_hal_mobile_get_lte_cell_info(pika_hal_mobile_lte_cell_info * out){
    luat_mobile_cell_info_t luat_cell_info;
    if (luat_mobile_get_cell_info(&luat_cell_info) < 0){
        return -1;
    }
    if (!luat_cell_info.lte_info_valid){
        return -1;
    }
    _copy_lte_cell_info(&luat_cell_info, out);
    return 0;
}

int pika_hal_mobile_get_gsm_cell_info(pika_hal_mobile_gsm_cell_info * out){
    luat_mobile_cell_info_t luat_cell_info;
    if (luat_mobile_get_cell_info(&luat_cell_info) < 0){
        return -1;
    }
    if (!luat_cell_info.gsm_info_valid){
        return -1;
    }
    _copy_gsm_cell_info(&luat_cell_info, out);
    return 0;
}

int pika_hal_mobile_get_cell_info(pika_hal_mobile_cell_info* out){
    luat_mobile_cell_info_t luat_cell_info = {0};
    if (luat_mobile_get_cell_info(&luat_cell_info) < 0){
        out->network_type = PIKA_HAL_MOBILE_NETWORK_UNKNOWN;
        return -1;
    }
    if (luat_cell_info.lte_info_valid){
        out->network_type = PIKA_HAL_MOBILE_NETWORK_LTE;
    }
    else if (luat_cell_info.gsm_info_valid){
        out->network_type = PIKA_HAL_MOBILE_NETWORK_GSM;
    }
    else{
        out->network_type = PIKA_HAL_MOBILE_NETWORK_UNKNOWN;
        return -1;
    }
    _copy_lte_cell_info(&luat_cell_info, &out->lte_cell_info);
    _copy_gsm_cell_info(&luat_cell_info, &out->gsm_cell_info);
    return 0;

}


int pika_hal_mobile_get_local_ip(int sim_id, int cid, ip_addr_t *ip_v4, ip_addr_t *ip_v6){
    return luat_mobile_get_local_ip(sim_id, cid, ip_v4, ip_v6) == 0 ? 0 : -1;
}

int pika_hal_mobile_get_csq(int* out){
    uint8_t _csq;
    if (luat_mobile_get_signal_strength(&_csq) < 0){
        return -1;
    }
    *out = _csq;
    return 0;
}

int pika_hal_mobile_get_rssi(int* out){
    luat_mobile_signal_strength_info_t info = {0};
    if (luat_mobile_get_signal_strength_info(&info) < 0) {
        return -1;
    }
    *out = info.lte_signal_strength.rssi;
    return 0;
}

void func(){
}